package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.labdata.domain.LabFile;
import com.ruoyi.labdata.domain.LabRepairedDevice;
import com.ruoyi.labdata.service.ILabFileService;
import com.ruoyi.labdata.service.ILabFlowInstanceService;
import com.ruoyi.labdata.service.ILabRepairedDeviceService;
import com.ruoyi.system.domain.ReceiptForm;
import com.ruoyi.system.mapper.ReceiptFormMapper;
import com.ruoyi.system.service.IReceiptFormService;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.utils.FileTypeEnum;

/**
 * 单据Service业务层处理
 * 
 * @author malang
 * @date 2022-02-02
 */
@Service
public class ReceiptFormServiceImpl implements IReceiptFormService {
	private static final String belongType = FileTypeEnum.Receipt.getValue();
	@Autowired
	private ILabFileService labFileService;
	@Autowired
	private ReceiptFormMapper receiptFormMapper;
	@Autowired
	private ILabFlowInstanceService labFlowInstanceService;
	@Autowired
	private ISysUserService sysUserService;
	@Autowired
	private ISysDictDataService sysDictDataService;
	@Autowired
	private ILabRepairedDeviceService labRepairedDeviceService;
	
	/**
	 * 查询单据
	 * 
	 * @param instancetId
	 *            单据ID
	 * @return 单据
	 */
	@Override
	public ReceiptForm selectReceiptFormById(Long instancetId) {

		List<LabFile> labFiles = labFileService.getFiles(instancetId, belongType);
		ReceiptForm receiptForm = receiptFormMapper.selectReceiptFormById(instancetId);
		if (ObjectUtil.isNull(receiptForm)){
			throw new RuntimeException("参数有误,没有id对应的数据");
		}
		receiptForm.setImages(labFiles);
		/*if(receiptForm.getReceiptTypeOpt()==4){
			receiptForm.setRepairedDevice(labRepairedDeviceService.selectLabRepairedDeviceById(instancetId));
		}*/
		return receiptForm;
	}
	
	public List<LabFile> getInstanceFiles(Long instancetId){
		return labFileService.getFiles(instancetId, belongType);
	}

	/**
	 * 查询单据列表
	 * 
	 * @param receiptForm
	 *            单据
	 * @return 单据
	 */
	@Override
	public List<ReceiptForm> selectReceiptFormList(ReceiptForm receiptForm) {
		SysUser loginUser = SecurityUtils.getLoginUser().getUser();
		receiptForm.setCreateBy(loginUser.getUserName());
		return receiptFormMapper.selectReceiptFormList(receiptForm);
	}

	/**
	 * 新增单据
	 * 
	 * @param receiptForm
	 *            单据
	 * @return 结果
	 */
	@Override
	@Transactional
	public int insertReceiptForm(ReceiptForm receiptForm) {
		SysUser user = sysUserService.selectUserByUserName(receiptForm.getCreateBy());
		receiptForm.setCreateOrgId(user.getDeptId());
		SysDictData dictData = sysDictDataService.selectDictData("receiptTypeOpt", receiptForm.getReceiptTypeOpt().toString());
		if(dictData.getRemark()==null){
			throw new RuntimeException("料单类型字典数据的备注请注明生成对应的申报号类型，如低值设备备注为：实低");
		}
		String applicationNo = dictData.getRemark()+receiptFormMapper.getSequence(dictData.getRemark()).toString()+"号";
		receiptForm.setApplicationNo(applicationNo);
		//前台输入
		//receiptForm.setCreateTime(new Date());
		labFlowInstanceService.insertLabFlowInstance(receiptForm);
		labFileService.insertFiles(receiptForm.getImages(), receiptForm.getInstanceId(), belongType);
		/*if(receiptForm.getReceiptTypeOpt()==4){
			LabRepairedDevice device = receiptForm.getRepairedDevice();
			device.setInstanceId(receiptForm.getInstanceId());
			labRepairedDeviceService.insertLabRepairedDevice(device);
		}*/
		return receiptFormMapper.insertReceiptForm(receiptForm);
	}

	/**
	 * 修改单据
	 * 
	 * @param receiptForm
	 *            单据
	 * @return 结果
	 */
	@Override
	@Transactional
	public int updateReceiptForm(ReceiptForm receiptForm) {
		SysUser user = sysUserService.selectUserByUserName(receiptForm.getCreateBy());
		receiptForm.setCreateOrgId(user.getDeptId());
		labFlowInstanceService.updateLabFlowInstance(receiptForm);
		labFileService.updateFiles(receiptForm.getImages(), receiptForm.getInstanceId(), belongType);
		/*if(receiptForm.getReceiptTypeOpt()==4){
			LabRepairedDevice device = receiptForm.getRepairedDevice();
			device.setInstanceId(receiptForm.getInstanceId());
			labRepairedDeviceService.updateLabRepairedDevice(device);
		}*/
		return receiptFormMapper.updateReceiptForm(receiptForm);
	}
	
	
	@Override
	@Transactional
	public int commitReceiptForm(ReceiptForm receiptForm) {
		this.updateReceiptForm(receiptForm);
		return labFlowInstanceService.commitFlow(receiptForm);
	}

	/**
	 * 批量删除单据
	 * 
	 * @param instancetIds
	 *            需要删除的单据ID
	 * @return 结果
	 */
	@Override
	@Transactional
	public int deleteReceiptFormByIds(Long[] instancetIds) {
		for (Long instrumentationId : instancetIds) {
			labFileService.deleteLabFile(instrumentationId, belongType);
		}
		labRepairedDeviceService.deleteLabRepairedDeviceByIds(instancetIds);
		labFlowInstanceService.deleteLabFlowInstanceByIds(instancetIds);
		return receiptFormMapper.deleteReceiptFormByIds(instancetIds);
	}

	/**
	 * 删除单据信息
	 * 
	 * @param instancetId
	 *            单据ID
	 * @return 结果
	 */
	@Override
	@Transactional
	public int deleteReceiptFormById(Long instanceId) {
		labFileService.deleteLabFile(instanceId, belongType);
		labFlowInstanceService.deleteLabFlowInstanceById(instanceId);
		labRepairedDeviceService.deleteLabRepairedDeviceById(instanceId);
		return receiptFormMapper.deleteReceiptFormById(instanceId);
	}
	
	public Integer getSequence(String maxKey){
		return receiptFormMapper.getSequence(maxKey);
	}
	
}
